package com.hhq.sso.system.controller;

import java.util.List;
import java.util.Map;

import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.system.entity.Dict;
import com.hhq.sso.system.service.IDictService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
@Tag(name = "字典", description = "字典")
public class DictController {

    private IDictService dictService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入dict")
    public CommonResult<Dict> detail(Dict dict) {
        Dict detail = dictService.getOne(Condition.getQueryWrapper(dict));
        return CommonResult.data(detail);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @Parameters({
        @Parameter(name = "code", description = "字典编号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "dictValue", description = "字典名称", in = ParameterIn.QUERY,
            schema = @Schema(type = "string"))})
    @Operation(summary = "列表", description = "传入dict")
    public CommonResult<List<Dict>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> dict) {
        List<Dict> list =
            dictService.list(Condition.getQueryWrapper(dict, Dict.class).lambda().orderByAsc(Dict::getSort));
        return CommonResult.data(list);
    }

    /**
     * 获取字典树形结构
     *
     * @return
     */
    @GetMapping("/tree")
    @Operation(summary = "树形结构", description = "树形结构")
    public CommonResult<List<Dict>> tree() {
        List<Dict> tree = dictService.tree();
        return CommonResult.data(tree);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入dict")
    public CommonResult<Boolean> submit(@Valid @RequestBody Dict dict) {
        return CommonResult.status(dictService.submit(dict));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "删除", description = "传入ids")
    public CommonResult<Boolean> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        return CommonResult.status(dictService.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 获取字典
     *
     * @return
     */
    @GetMapping("/dictionary")
    @Operation(summary = "获取字典", description = "获取字典")
    public CommonResult<List<Dict>> dictionary(String code) {
        List<Dict> tree = dictService.getList(code);
        return CommonResult.data(tree);
    }

}
